using UnityEngine;
using System.Collections;

public class EnemyAttack : MonoBehaviour {
	
public GameObject target;
	public float attacktimer;
	public float coolDown;
	
	private float DistanceToTar;

	// Use this for initialization
	void Start () {
		attacktimer = 0.0f;
		coolDown = 3.0f;
	}
	
	// Update is called once per frame
	void Update () {
		DistanceToTar = Vector3.Distance(target.transform.position, transform.position);
		
		if(DistanceToTar < 2.5f)
		{
			if(attacktimer > 0)
			{
				attacktimer -= Time.deltaTime;
			}
			if(attacktimer < 0)
			{
				attacktimer = 0;
			}
	
			if(attacktimer == 0)
			{
				Attack();
				attacktimer = coolDown;
			}
		}
	}
	
	private void Attack() {
		//float distance = Vector3.Distance(target.transform.position, transform.position);
		Vector3 dir = (target.transform.position - transform.position).normalized;
		
		float direction = Vector3.Dot(dir, transform.forward);
		
		//Debug.Log(DistanceToTar);
		
		if(DistanceToTar < 2.5f)
		{
			if(direction > 0)
			{
				PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
				ph.AdjustCurrentHealth(-10);
			}
		}
	}
}
